package com.travel.server.manager.impl.command.article;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.article.data.QuerySingleArticleDataRequest;
import com.travel.server.manager.dto.response.article.QueryArticleListResponse;
import com.travel.server.manager.dto.response.article.QueryArticleListResponse.ArticleItem;
import com.travel.server.manager.dto.response.article.data.QuerySingleArticleDataResponse;
import com.travel.server.manager.dto.response.article.data.QuerySingleArticleDataResponse.ArticleDataItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:26
 * @Desc:
 */
@Component
public class QuerySingleArticleDataCommand extends AbstractCommand<QuerySingleArticleDataRequest, QuerySingleArticleDataResponse> {

    @Autowired
    private QueryArticleListCommand command;

    @Override
    protected void checkInput(QuerySingleArticleDataRequest request) {
        checkPageInput(request);
    }

    @Override
    public QuerySingleArticleDataResponse handle(QuerySingleArticleDataRequest request) {
        QuerySingleArticleDataResponse response = new QuerySingleArticleDataResponse();

        QueryArticleListResponse listResponse = command.handle(request);
        List<ArticleItem> articleItemList = listResponse.getArticleList();
        List<ArticleDataItem> articleDataItemList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(articleItemList)){
            for(ArticleItem articleItem : articleItemList){
                ArticleDataItem item = new ArticleDataItem();
                item.setClassName(articleItem.getClassName());
                item.setName(articleItem.getTitle());
                item.setBrowser(0);
                item.setExpose(0);
                item.setVisit(0);
                articleDataItemList.add(item);
            }
        }

        response.setTotalNum(listResponse.getTotalNum());
        response.setArticleList(articleDataItemList);
        return response;
    }
}
